// Copyright (c) 2024 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Based on direct_shared_image_video_provider.h originally written by
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. 

#ifndef MEDIA_GPU_OHOS_DIRECT_SHARED_IMAGE_VIDEO_PROVIDER_H_
#define MEDIA_GPU_OHOS_DIRECT_SHARED_IMAGE_VIDEO_PROVIDER_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
#include "gpu/command_buffer/service/ohos/native_image_texture_owner.h"
#include "gpu/command_buffer/service/ref_counted_lock.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/ipc/service/command_buffer_stub.h"
#include "media/base/video_frame.h"
#include "media/gpu/media_gpu_export.h"
#include "media/gpu/ohos/codec_image.h"
#include "media/gpu/ohos/shared_image_video_provider.h"
#include "media/gpu/ohos/video_frame_factory.h"
#include "ui/gl/gl_bindings.h"

namespace media {
class GpuSharedImageVideoFactory;

class MEDIA_GPU_EXPORT DirectSharedImageVideoProvider
    : public SharedImageVideoProvider,
      public gpu::RefCountedLockHelperDrDc {
 public:
  DirectSharedImageVideoProvider(
      scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner,
      GetStubCB get_stub_cb,
      scoped_refptr<gpu::RefCountedLock> drdc_lock);

  DirectSharedImageVideoProvider(const DirectSharedImageVideoProvider&) =
      delete;
  DirectSharedImageVideoProvider& operator=(
      const DirectSharedImageVideoProvider&) = delete;

  ~DirectSharedImageVideoProvider() override;

  void Initialize(GpuInitCB get_stub_cb) override;
  void RequestImage(ImageReadyCB cb, const ImageSpec& spec) override;

 private:
  base::SequenceBound<GpuSharedImageVideoFactory> gpu_factory_;
  scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
};

class GpuSharedImageVideoFactory
    : public gpu::CommandBufferStub::DestructionObserver {
 public:
  explicit GpuSharedImageVideoFactory(
      SharedImageVideoProvider::GetStubCB get_stub_cb);

  GpuSharedImageVideoFactory(const GpuSharedImageVideoFactory&) = delete;
  GpuSharedImageVideoFactory& operator=(const GpuSharedImageVideoFactory&) =
      delete;

  ~GpuSharedImageVideoFactory() override;

  void Initialize(SharedImageVideoProvider::GpuInitCB init_cb);

  using FactoryImageReadyCB = SharedImageVideoProvider::ImageReadyCB;

  void CreateImage(FactoryImageReadyCB cb,
                   const SharedImageVideoProvider::ImageSpec& spec,
                   scoped_refptr<gpu::RefCountedLock> drdc_lock);

 private:
  bool CreateImageInternal(const SharedImageVideoProvider::ImageSpec& spec,
                           gpu::Mailbox mailbox,
                           scoped_refptr<CodecImage> image,
                           scoped_refptr<gpu::RefCountedLock>);

  void OnWillDestroyStub(bool have_context) override;

  raw_ptr<gpu::CommandBufferStub> stub_ = nullptr;

  THREAD_CHECKER(thread_checker_);

  bool is_vulkan_ = false;

  base::WeakPtrFactory<GpuSharedImageVideoFactory> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_GPU_OHOS_DIRECT_SHARED_IMAGE_VIDEO_PROVIDER_H_
